﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyFitness.View;
using MyFitness.Model;

namespace MyFitness.View
{
    public partial class TraineeAccountProfileForm : Form
    {
        #region Declared Variables
        private Trainee trainee;
        private bool newaccount = false;
        #endregion

        /// <summary>
        /// Constructor for New Accounts
        /// </summary>
        public TraineeAccountProfileForm()
        {
            newaccount = true;
            trainee = new Model.Trainee();
            trainee.BirthDate = DateTime.Now;
            InitializeComponent();
            CenterToScreen();
            okButton.Text = "Sign Up";
            okButton.Enabled = false;

            // Create handlers for tracking when trainee information is validated for a new account
            EmailTextBox.TextChanged += new EventHandler(OnEnableOKButton);
            PasswordTextBox.TextChanged += new EventHandler(OnEnableOKButton);
            LastNameTextBox.TextChanged += new EventHandler(OnEnableOKButton);
            FirstNameTextBox.TextChanged += new EventHandler(OnEnableOKButton);
            GenderComboBox.TextChanged += new EventHandler(OnEnableOKButton);
        }

        /// <summary>
        /// Constructor with Trainee Input
        /// </summary>
        /// <param name="t">Trainee Object</param>
        public TraineeAccountProfileForm(Model.Trainee t)
        {
            // Ensures a valid trainee was passed into the function otherwise the form will exit.
            if (t != null)
            {
                trainee = t;
                InitializeComponent();
                CenterToScreen();
            }
            else
                Close();
        }

        /// <summary>
        /// Account Profile Form Load Form Call
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TraineeAccountProfileForm_Load(object sender, EventArgs e)
        {
            // Disables the EmailTextBox for established accounts.
            if( newaccount != true )
                EmailTextBox.Enabled = false;

            // If the email is not set yet, then it is a new user.
            if (trainee.Email != null)
            {
                EmailTextBox.Text = trainee.Email;
                PasswordTextBox.Text = trainee.Password;
                FirstNameTextBox.Text = trainee.FirstName;
                LastNameTextBox.Text = trainee.LastName;
                GenderComboBox.Text = trainee.Gender;
                BirthdateDateTimePicker.Value = trainee.BirthDate;
            }
        }

        /// <summary>
        /// Enables or Disables the OKButton depending on validated input.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnEnableOKButton(object sender, EventArgs e)
        {
            // Enables the "OK" Button if all the fields are validated, i.e. has a first and last name...
            if (LastNameTextBox.Text.Length > 0 && FirstNameTextBox.Text.Length > 0 && GenderComboBox.Text.Length > 0)
                okButton.Enabled = true;
            else
                okButton.Enabled = false;
        }

        private void EmailTextBox_TextChanged(object sender, EventArgs e)
        {
            trainee.Email = EmailTextBox.Text;
        }

        private void PasswordTextBox_TextChanged(object sender, EventArgs e)
        {
            trainee.Password = PasswordTextBox.Text;
        }

        private void FirstNameTextBox_TextChanged(object sender, EventArgs e)
        {
            trainee.FirstName = FirstNameTextBox.Text;
        }

        private void LastNameTextBox_TextChanged(object sender, EventArgs e)
        {
            trainee.LastName = LastNameTextBox.Text;
        }

        private void GenderComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            trainee.Gender = GenderComboBox.Text;
        }

        private void GenderComboBox_TextChanged(object sender, EventArgs e)
        {
            trainee.Gender = GenderComboBox.Text;
        }

        private void BirthdateDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            trainee.BirthDate = BirthdateDateTimePicker.Value;
        }

        #region Accessors
        /// <summary>
        /// Returns a Trainee Object from the data in the form.
        /// </summary>
        public Trainee Trainee
        {
            get
            {
                return trainee;
            }
        }

        /// <summary>
        /// Returns true if a new account is being created.  Otherwise false.
        /// </summary>
        public bool IsNewAccount
        {
            get
            {
                return newaccount;
            }
        }
        #endregion
    }
}
